package com.example.managesystem.web;

import com.example.managesystem.entity.*;
import com.example.managesystem.service.CityService;
import com.example.managesystem.service.UserService;
import com.example.managesystem.service.imageService;
import com.example.managesystem.service.impl.postServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("index")
public class userController {
    @Autowired
    private postServiceImpl PostServiceImpl;
    @Autowired
    private imageService imageService;
    @Autowired
    private UserService UserService;
    @Autowired
    private CityService CityService;

    /**
     * @description:  重定向注册页面，增加返回消息
     * @param: [user]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:29
     */
    @RequestMapping("/register")
    public String register(user user, RedirectAttributes redirectAttributes){
        if(UserService.add(user)!="注册失败"){
            return "redirect:/index/login-33";
        }
        else{
            redirectAttributes.addFlashAttribute("message", "注册失败");
            return "redirect:/index/register-33";
        }
    }

    /**
     * @description:  注册
     * @param: [mod]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/23 23:10
     */
    @RequestMapping(value = "/register-33")
    public String register(ModelMap mod){
        String message = (String) mod.getAttribute("message");
        List<city> list = CityService.selectCity();
        mod.put("CityList",list);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("message", message);
        return "/register-33";
    }

    /**
     * @description:  登入页面
     * @param: [user, mod]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:29
     */
    @RequestMapping("/main")
    public String index(user user, ModelMap mod, RedirectAttributes redirectAttributes, HttpSession session) {
        if (UserService.selectUser(user)!=null){
            mod.addAttribute("UserInfo",UserService.selectUser(user).get(0));
            session.setAttribute("author",user.getEmail());
            session.setAttribute("headImage",UserService.selectUser(user).get(0).getImage());
            return "/main";
        }else {
            redirectAttributes.addFlashAttribute("message", "账号或密码错误");
            return "redirect:/index/login-33";
        }
    }

    /**
     * @description:  重定向，返回登入成功与否信息
     * @param: [mod]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/23 23:48
     */
    @RequestMapping("/login-33")
    public String login(ModelMap mod){
        String message = (String) mod.getAttribute("message");
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("message", message);
        return "/login-33";
    }

    /**
     * @description:  查询页面
     * @param: [mod]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:29
     */
    @RequestMapping("/tables")
    public String user(ModelMap mod){
        List<user> list = UserService.selectAll();
        mod.put("UserList",list);
        return "/tables";
    }

    /**
     * @description:  重定向到原来的页面
     * @param: [name, mod]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:30
     */
    @RequestMapping("/user-profile-lite/{name}")
    public String user(@PathVariable("name") String name,ModelMap mod){
        user user = UserService.selectUserInfo(name);
        mod.addAttribute("UserInfo",user);
        List<headImages> list1=imageService.selectImages();
        List<city> list = CityService.selectCity();
        mod.addAttribute("imageList",list1);
        mod.put("CityList",list);
        return "/user-profile-lite";
    }

    /**
     * @description:  更新User数据
     * @param: [user]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:30
     */
    @RequestMapping(value = "/Update")
    public String Update(user user){
        if(UserService.Update(user)==1){
            Object str = user.getEmail();
            String URL="redirect:/index/user-profile-lite/"+str;
            //重新定向
            return URL;
        }else {
            return "/errors";
        }
    }

}
